#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

void display(const list<int> &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void test()
{
    list<int> number = {1, 4, 6, 8, 9, 10, 5, 7};
    display(number);//遍历list

    /* cout << "list的随机访问" << endl; */
    /* cout << "number[1] = " << number[1] << endl;//error,不支持,这句话编译不通过 */

    cout << endl << "在list的尾部进行插入" << endl;
    number.push_back(22);
    number.push_back(33);
    display(number);//遍历list

    cout << endl;

    cout << endl << "在list的尾部进行删除" << endl;
    number.pop_back();
    display(number);//遍历list
    cout << endl;

    cout << endl << "在list的头部进行插入" << endl;
    number.push_front(111);
    number.push_front(222);
    display(number);//遍历list

    cout << endl << "在list的头部进行删除" << endl;
    number.pop_front();
    display(number);//遍历list
                    
    cout << endl << "在list的任意位置进行插入" << endl;
    auto it = number.begin();
    ++it;
    cout << "*it = " << *it << endl;
    //在第二个元素的前面插入元素300
    number.insert(it, 300);
    display(number);//遍历list
                    
    cout << endl << "在list的任意位置进行删除" << endl;
    it = number.end();//指向最后一个元素的下一个位置
    --it;
    --it;
    cout << "*it = " << *it << endl;
    //删除倒数第二个元素
    number.erase(it);
    display(number);//遍历list
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

